using System.Collections;
using UnityEngine;

public class Weapon : MonoBehaviour
{
	public int id;

	public string itemName;

	public int priceCoin;

	public float priceInapp;

	public int baseAmmo;

	public int priceUpgrade;

	public int upgradeAmmo;

	public string sprName;

	public string sku;

	public float damage;

	public float shootCooldown;

	public float recoilForce;

	private float sinceShoot;

	[SerializeField]
	private Weapon_Attack_Type attackType;

	private bool canShoot;

	public Transform muzzleTransform;

	public Bullet bulletPrefab;

	public Grenade grenadePrefab;

	public CharacterInformation owner;

	private int currentAmmo;

	private ParticleSystem muzzleFlash;

	[Header("For multiple shot")]
	[SerializeField]
	private int multipleShotCount;

	[Header("For burst shot")]
	[SerializeField]
	private int burstShotCount;

	private AudioSource source;

	public AudioClip shootSound;

	public Collider[] cols;

	public bool CanBuyWithCoin => priceCoin > 0;

	public bool CanBuyWithInapp => priceInapp > 0f;

	public string PrefKey => "Weapon" + id;

	public int Level
	{
		get
		{
			return PlayerPrefs.GetInt(PrefKey, 0);
		}
		set
		{
			value = Mathf.Clamp(value, 0, 9);
			PlayerPrefs.SetInt(PrefKey, value);
		}
	}

	public int MaxAmmo => baseAmmo + Level * upgradeAmmo;

	public Rigidbody body => GetComponent<Rigidbody>();

	private void Awake()
	{
		source = base.gameObject.AddComponent<AudioSource>();
		source.playOnAwake = false;
		source.loop = false;
		cols = GetComponentsInChildren<Collider>();
		Renderer componentInChildren = GetComponentInChildren<Renderer>();
		if (componentInChildren != null)
		{
			componentInChildren.sortingLayerName = "Top";
			componentInChildren.sortingOrder = 100;
		}
	}

	private void Start()
	{
		body.maxAngularVelocity = 15f;
		currentAmmo = MaxAmmo;
		muzzleFlash = GetComponentInChildren<ParticleSystem>();
		muzzleFlash.useAutoRandomSeed = true;
	}

	private void Update()
	{
		sinceShoot += Time.deltaTime;
		canShoot = (sinceShoot > shootCooldown);
	}

	private void FixedUpdate()
	{
		if (owner != null)
		{
			float d = (!owner.GetComponent<CharacterRotation>().lookingRight) ? (-1f) : 1f;
			Vector3 vector = Vector3.Cross(base.transform.right * d, -Vector3.right);
			float d2 = Vector3.Angle(base.transform.up, vector);
			Vector3 a = Vector3.Cross(base.transform.up, vector);
			body.AddTorque(d2 * a * 500f, ForceMode.Acceleration);
		}
	}

	public bool Shoot(Vector3 target)
	{
		if (canShoot && currentAmmo > 0)
		{
			if (PersistenData.Sound)
			{
				source.PlayOneShot(shootSound);
			}
			muzzleFlash.Play();
			switch (attackType)
			{
			case Weapon_Attack_Type.Single:
				currentAmmo--;
				SingleShoot();
				break;
			case Weapon_Attack_Type.Multiple:
				currentAmmo--;
				MultipleShoot(multipleShotCount);
				break;
			case Weapon_Attack_Type.Burst:
				currentAmmo -= burstShotCount;
				BurstShoot(burstShotCount);
				break;
			case Weapon_Attack_Type.Grenade:
				currentAmmo--;
				GrenadeShoot();
				break;
			case Weapon_Attack_Type.Bounce:
				currentAmmo--;
				BounceShoot();
				break;
			default:
				UnityEngine.Debug.LogWarning("Why here?");
				break;
			}
			sinceShoot = 0f;
			if (currentAmmo <= 0)
			{
				DropAndDestroy();
			}
			return true;
		}
		return false;
	}

	private void SingleShoot()
	{
		Bullet bullet = UnityEngine.Object.Instantiate(bulletPrefab, muzzleTransform.position, muzzleTransform.rotation);
		bullet.SetBullet(damage, owner);
		RecoilForce();
	}

	private void MultipleShoot(int count)
	{
		RecoilForce();
		for (int i = 0; i < count; i++)
		{
			Bullet bullet = UnityEngine.Object.Instantiate(bulletPrefab, muzzleTransform.position, muzzleTransform.rotation);
			bullet.SetBullet(damage, owner);
			bullet.transform.Rotate(new Vector3((i - count / 2) * 10, 0f, 0f));
		}
		owner.mainRig.AddForce(-muzzleTransform.forward * recoilForce * 20f, ForceMode.Impulse);
	}

	private void BurstShoot(int count)
	{
		StartCoroutine(coroBurstShot(count));
	}

	private IEnumerator coroBurstShot(int count)
	{
		RecoilForce();
		for (int i = 0; i < count; i++)
		{
			body.AddForce(-muzzleTransform.forward * 30f, ForceMode.Impulse);
			Bullet b = UnityEngine.Object.Instantiate(bulletPrefab, muzzleTransform.position, muzzleTransform.rotation);
			b.SetBullet(damage, owner);
			yield return new WaitForSeconds(0.05f);
		}
	}

	private void GrenadeShoot()
	{
		Grenade grenade = UnityEngine.Object.Instantiate(grenadePrefab, muzzleTransform.position, muzzleTransform.rotation);
		grenade.SetBullet(damage, owner);
		RecoilForce();
		grenade.body.useGravity = true;
		grenade.body.AddForce(grenade.transform.forward * 15f, ForceMode.Impulse);
	}

	private void BounceShoot()
	{
		Bullet bullet = UnityEngine.Object.Instantiate(bulletPrefab, muzzleTransform.position, muzzleTransform.rotation);
		bullet.SetBullet(damage, owner);
		RecoilForce();
	}

	private void RecoilForce()
	{
		body.AddForce(-muzzleTransform.forward * 20f, ForceMode.Impulse);
		owner.holderRig.AddForce(-muzzleTransform.forward * recoilForce, ForceMode.Impulse);
	}

	private void DropAndDestroy()
	{
		owner.GetComponent<CharacterFighting>().DropWeapon();
		UnityEngine.Object.Destroy(base.gameObject, 1f);
	}

	private void OnDrawGizmos()
	{
		Gizmos.color = Color.red;
		Gizmos.DrawWireSphere(base.transform.position, 0.1f);
	}
}
